package com.pureut.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.domain.SupplierElimination;
import com.pureut.system.domain.SupplierEvaluation;
import com.pureut.system.domain.dto.SupplierEliminationPagesDto;
import com.pureut.system.domain.vo.SupplierEliminationVo;
import com.pureut.system.mapper.SupplierEliminationMapper;
import com.pureut.system.service.ISupplierEliminationService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * @Author: pureut
 * @Date: 2024-12-05 17:16:19
 * @Desc: 供应商淘汰
 */
@Service("supplierEliminationService")
public class SupplierEliminationServiceImpl extends ServiceImpl<com.pureut.system.mapper.SupplierEliminationMapper, SupplierElimination> implements ISupplierEliminationService {

    /**
     * 供应商淘汰Dao
     */
    @Resource
    private SupplierEliminationMapper supplierEliminationMapper;

    @Resource
    FeignService feignService;

    /**
     * 查询列表
     * @param supplierEliminationVo
     * @return
     */
    @Override
    public List<SupplierEliminationPagesDto> queryPage(SupplierEliminationVo supplierEliminationVo) {
        // 获取认证方式的数据字典
        List<SysDictData> netArray = DictUtils.getDictCache("supplier_type");
        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        // 获取认证方式的数据字典
        List<SysDictData> netArray2 = DictUtils.getDictCache("transaction_type");
        Map<String, String> netMap2 = netArray2.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        List<SupplierEliminationPagesDto> supplierEliminationPagesDtos = supplierEliminationMapper.selectByPage(supplierEliminationVo);
        supplierEliminationPagesDtos.forEach(e->{
            e.setSupplierCategory(netMap.get(String.valueOf(e.getSupplierCategory())));
            e.setTransactionType(netMap2.get(String.valueOf(e.getTransactionType())));

        });
        return supplierEliminationPagesDtos;
    }


    /**
     * 新增供应商淘汰
     * @param supplierEliminationVo
     * @return
     */
    @Override
    public Boolean save(SupplierEliminationVo supplierEliminationVo) {
        SupplierElimination supplierElimination = new SupplierElimination();

        BeanUtil.copyProperties(supplierEliminationVo, supplierElimination);
        String sn = feignService.getAuthorityCoding("warehouse:outbound:reworksto:list");
        if (sn == null) {
            throw new GlobalException("该单据未进行配置,无法生成单据编码");
        }
        supplierElimination.setDocNum(sn);
        supplierElimination.setStatus("1");
        supplierElimination.setCreateBy(SecurityUtils.getUsername());
        supplierElimination.setCreateTime(new Date());
        return save(supplierElimination);
    }


    /**
     * 更新供应商淘汰
     * @param supplierEliminationVo
     * @return
     */
    @Override
    public Boolean updateById(SupplierEliminationVo supplierEliminationVo) {
        SupplierElimination supplierElimination = new SupplierElimination();
        BeanUtil.copyProperties(supplierEliminationVo, supplierElimination);
        supplierElimination.setUpdateBy(SecurityUtils.getUsername());
        supplierElimination.setUpdateTime(new Date());
        return updateById(supplierElimination);
    }

    @Override
    public Boolean submit(Long id){
        SupplierElimination supplierAccess = new SupplierElimination();
        supplierAccess.setId(id);
        supplierAccess.setStatus("2");
        return updateById(supplierAccess);
    }

    @Override
    public Boolean examine(SupplierEliminationVo supplierEliminationVo){
        SupplierElimination supplierAccess = new SupplierElimination();
        supplierAccess.setId(supplierEliminationVo.getId());
        if(supplierEliminationVo.getAuditState()==1){
            supplierAccess.setStatus("3");
        }else{
            supplierAccess.setStatus("4");
        }
//        supplierAccess.set(SecurityUtils.getUsername());
//        supplierAccess.setApprovalTime(new Date());
        boolean b = updateById(supplierAccess);
        return b;
    }
}
